package com.crm.controller.hdj;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.security.sasl.Sasl;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanGenerator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.model.jdao.Icustomer;
import com.crm.model.jdao.IstaffH;
import com.crm.model.pojo.Contact;
import com.crm.model.pojo.Customer;
import com.crm.model.pojo.Detailedrequirement;
import com.crm.model.pojo.Salesopportunity;
import com.crm.services.salesopportunityServices;
import com.github.pagehelper.PageInfo;

@Controller

public class salaoppcontroller {
	@Autowired
	salesopportunityServices saser;
	@Autowired
	Icustomer icustomer;
	@Autowired
	IstaffH staff;
	@PostMapping("saget")
	@ResponseBody
	public  JSONObject m() {
		try {
			List<Salesopportunity> aa = saser.bb();
			
			PageInfo<Salesopportunity> page = new PageInfo<>(aa);
			JSONObject jsonObject = (JSONObject)JSONObject.toJSON(page);
			return jsonObject;
		} catch (Exception e) {
			System.out.println("cuowuxinxi");
			System.out.println(e.getMessage());
		}
		return null;
		
	}
	
	@PostMapping("xianxixuqiu")
	@ResponseBody
	public  JSONObject xian() {
		try {
			List<Detailedrequirement> aa = saser.cc();
			for (Detailedrequirement detailedrequirement : aa) {
				System.out.println(detailedrequirement.getImportancedegree());
			}
			PageInfo<Detailedrequirement> page = new PageInfo<>(aa);
			JSONObject jsonObject = (JSONObject)JSONObject.toJSON(page);
			return jsonObject;
		} catch (Exception e) {
			System.out.println("cuowuxinxi");
			System.out.println(e.getMessage());
		}
		return null;
		
	}
	
	@RequestMapping("addxuqiu")
	@ResponseBody
	public String addxuqiu(HttpServletRequest request) {
		String demandtheme = request.getParameter("demandtheme");
		String demandprovider = request.getParameter("demandprovider");
		String customer = request.getParameter("customer");
		String correspondingopp = request.getParameter("correspondingopp");
		String recordtime = request.getParameter("recordtime");
		String importancedegree = request.getParameter("importancedegree");
		String demandcontent = request.getParameter("demandcontent");
		Detailedrequirement de=new Detailedrequirement();
		if(demandtheme!="") {
			de.setDemandtheme(demandtheme);
		}
		if(demandprovider!="") {
			de.setDemandprovider(demandprovider);
		}
		if(customer!="") {
			de.setCustomerBean(icustomer.findById(customer).get());
		}
		if(correspondingopp!="") {
			de.setCorrespondingOpp(correspondingopp);
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date date;
		if(recordtime!="") {
		try {
			date = formatter.parse(recordtime);
			de.setRecordtime(date);
		} catch (ParseException e) {
			e.printStackTrace();
			System.out.println("expect_time转换错误");
		}
		}
		if(importancedegree!="") {
			de.setImportancedegree(Integer.parseInt(importancedegree));
		}
		if(demandcontent!="") {
			de.setDemandcontent(demandcontent);
		}
		saser.adddeat(de);
		return "1";
		
	}
	
	
	@RequestMapping("add")
	@ResponseBody
	public String add(HttpServletRequest request) {
		String salestheme = request.getParameter("salestheme");
		String cid = request.getParameter("cid");
		String type = request.getParameter("type");
		String personcharge = request.getParameter("personcharge");
		String expect_time = request.getParameter("expect_time");
		String possibility = request.getParameter("possibility");
		String state = request.getParameter("state");
		String update_time = request.getParameter("update_time");
		String source = request.getParameter("source");
		String expectmony = request.getParameter("expectmony");
		String first = request.getParameter("first");
		String starstandard = request.getParameter("starstandard");
		Salesopportunity saa=new Salesopportunity();
		if(salestheme!="") {
			saa.setSalestheme(salestheme);
		}
		if(cid!="") {
			saa.setCustomerBean(icustomer.findById(cid).get());
		}
		if(type!="") {
			saa.setType(type);
		}
		if(personcharge!="") {
			saa.setStaff(staff.findById(personcharge).get());
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date date;
		if(expect_time!="") {
		try {
			date = formatter.parse(expect_time);
			saa.setExpecttime(date);
		} catch (ParseException e) {
			e.printStackTrace();
			System.out.println("expect_time转换错误");
		}
		}
		if(source!="") {
			saa.setSource(source);
		}
		if(expectmony!="") {
			saa.setExpectmony(new BigDecimal(expectmony));
		}
		if(first!="") {
			saa.setFirst(Integer.parseInt(first));
		}
		if(starstandard!="") {
			saa.setStarstandard(Integer.parseInt(starstandard));
		}
		saser.addsalopp(saa);
		return "1";
		
	}
	
	
	
	@PostMapping("newsalopp")
	@ResponseBody
	public  JSONArray newsalopp(HttpServletRequest request) {
		JSONArray array =new JSONArray();
		List<Customer> aa = (List<Customer>) saser.selectallkehu();
		
		for(int i=0; i<aa.size();i++) {
		JSONObject obj = new JSONObject();
		obj.put("cname", aa.get(i).getCname());
		obj.put("cid", aa.get(i).getCid());
		array.add(obj);
	}
		request.getSession().setAttribute("kehu",array);
		return array;
	}
	
	
	
	
	
	@RequestMapping("sa")
	public String sa() {
		return "shouqian/EmementUi";
	}
	
	@RequestMapping("xuqiu")
	public String xuqiu() {
		return "shouqian/kehuxuqiu";
	}
	
	@GetMapping("indextow")
	public String indext() {
		return "indextow";
	}
	
	@GetMapping("xinjian")
	public String xinjian(HttpServletRequest request) {
		JSONArray array =new JSONArray();
		JSONArray array1 =new JSONArray();
		List<Customer> aa = (List<Customer>) saser.selectallkehu();
		List<Contact> bb = saser.selectalllianxiren();
		for(int i=0; i<bb.size();i++) {
			JSONObject obj1 = new JSONObject();
			obj1.put("tname", bb.get(i).getTname());
			obj1.put("tid", bb.get(i).getTid());
			array1.add(obj1);
		}
		for(int i=0; i<aa.size();i++) {
		JSONObject obj = new JSONObject();
		obj.put("cname", aa.get(i).getCname());
		obj.put("cid", aa.get(i).getCid());
		array.add(obj);
	}
		request.getSession().setAttribute("lianxiren",array1);
		request.getSession().setAttribute("kehu",array);
		return "shouqian/newSalae";
	}
	@GetMapping("xinjianDetaiquirements")
	public String xinjianDetaiquirements(HttpServletRequest request) throws ParseException {
		JSONArray array =new JSONArray();
		JSONArray array1 =new JSONArray();
		List<Customer> aa = (List<Customer>) saser.selectallkehu();
		List<Salesopportunity> bb = saser.bb();
		
		for(int i=0; i<bb.size();i++) {
			JSONObject obj = new JSONObject();
			obj.put("salesid", bb.get(i).getSalesid());
			obj.put("salestheme", bb.get(i).getSalestheme());
			array1.add(obj);
		}
		
		for(int i=0; i<aa.size();i++) {
		JSONObject obj = new JSONObject();
		obj.put("cname", aa.get(i).getCname());
		obj.put("cid", aa.get(i).getCid());
		array.add(obj);
	}

		request.getSession().setAttribute("kehu",array);
		request.getSession().setAttribute("jihuizhutu",array1);
		return "shouqian/newDetailedrequirements";
	}
	
	@GetMapping("xiugai")
	public String xiugai(HttpServletRequest request) {
		String salestheme = request.getParameter("salestheme");
		String state = request.getParameter("state");
		String expectmony = request.getParameter("expectmony");
		String possibility = request.getParameter("possibility");
		String starstandard = request.getParameter("starstandard");
		String type = request.getParameter("type");
		String first = request.getParameter("first");
		String id = request.getParameter("id");
		Salesopportunity saa=new Salesopportunity();
		if(id!="") {
			saa.setSalesid(id);
		}
		if(salestheme!="") {
			saa.setSalestheme(salestheme);
		}
		if(state!="") {
			saa.setState(Integer.parseInt(state));
		}
		if(expectmony!="") {
			saa.setExpectmony(new BigDecimal(expectmony));
		}
		if(possibility!="") {
			saa.setPossibility(possibility);
		}
		if(starstandard!="") {
			saa.setStarstandard(Integer.parseInt(starstandard));
		}
		if(type!="") {
			saa.setType(type);
		}
		if(first!=""||first!="*") {
			saa.setFirst(Integer.parseInt(first));
		}
		saser.xiugai(saa);
		return "shouqian/newSalae";
		//return "shouqian/newBianjiShouqian";
	}
	
	
	
	@GetMapping("bianji")
	public String bianji(HttpServletRequest request) {
		String salesid = request.getParameter("salesid");
		String salestheme = request.getParameter("salestheme");
		String state = request.getParameter("state");
		String expectmony = request.getParameter("expectmony");
		String possibility1 = request.getParameter("possibility1");
		String starstandard = request.getParameter("starstandard");
		String first = request.getParameter("first");
		String type = request.getParameter("type");
		List<Salesopportunity> list=new ArrayList<Salesopportunity>();
		Salesopportunity sa=new Salesopportunity();
		String possibility12=possibility1+"%";
		sa.setSalesid(salesid);
		sa.setSalestheme(salestheme);
		if(state!=""||state!="*") {
			sa.setState(Integer.parseInt(state));
		}
		System.out.println("expectmony:"+expectmony);
		if(expectmony!=""||expectmony!="*") {
			//sa.setExpectmony(new BigDecimal(expectmony));
			System.out.println(expectmony.length()>0);
			System.out.println(expectmony.length());
			sa.setExpectmony(new BigDecimal(expectmony));
			
		}
		if(possibility1!=null) {
			sa.setPossibility(possibility12);
		}
		if(!("".equals(starstandard))||starstandard!="*") {
		sa.setStarstandard(Integer.parseInt(starstandard));
		}
		//System.out.println("".equals(first));
		//System.out.println(first);
		if(!("".equals(first))||first!="*") {
			sa.setFirst(Integer.parseInt(first));
		}
		sa.setType(type);
		sa.setSalesid(salesid);
		list.add(sa);
		request.getSession().setAttribute("list",list);
		//System.out.println("ks:"+list.get(0).getFirst());
		
		return "shouqian/newBianjiShouqian";
	}
	
	@GetMapping("bianjixuqiu")
	public String bianjixuqiu(HttpServletRequest request) {
		String detaliid = request.getParameter("detaliid");
		String demandtheme = request.getParameter("demandtheme");
		String demandprovider = request.getParameter("demandprovider");
		String customer = request.getParameter("customer");
		String correspondingopp = request.getParameter("correspondingopp");
		String recordtime = request.getParameter("recordtime");
		String importancedegree = request.getParameter("importancedegree");
		String demandcontent = request.getParameter("demandcontent");
		Detailedrequirement ddd=new Detailedrequirement();
		List<Detailedrequirement> list=new ArrayList<Detailedrequirement>();
		if(detaliid!="") {
			ddd.setDetaliid(detaliid);
		}
		if(demandtheme!="") {
			ddd.setDemandtheme(demandtheme);
		}
		if(demandprovider!="") {
			ddd.setDemandprovider(demandprovider);
		}
		System.out.println(customer);
		if(customer!="") {
			ddd.setCustomerBean(icustomer.findById(customer).get());
		}
		if(correspondingopp!="") {
			ddd.setCorrespondingOpp(correspondingopp);
		}
		if(importancedegree!="") {
			ddd.setImportancedegree(Integer.parseInt(importancedegree));
		}
		if(demandcontent!="") {
			ddd.setDemandcontent(demandcontent);
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date date;
		System.out.println("recordtime:"+recordtime);
		if(recordtime!="") {
		try {
			date = formatter.parse(recordtime);
			ddd.setRecordtime(date);
		} catch (ParseException e) {
			e.printStackTrace();
			System.out.println("expect_time转换错误");
		}
		}
		/*System.out.println(customer);
		List<Salesopportunity> bb = saser.dsadas(customer);
		for (Salesopportunity salesopportunity : bb) {
			System.out.println(salesopportunity.getExpecttime());
		}*/
		list.add(ddd);
		request.getSession().setAttribute("list",list);
		return "shouqian/bianjiDetailedrequirements";
	}
	
	//编辑需求保存
	@GetMapping("bianjixuqiu1")
	public String bianjixuqiu1(HttpServletRequest request) {
		String demandtheme = request.getParameter("demandtheme");
		String customer = request.getParameter("customer");
		String correspondingopp = request.getParameter("correspondingopp");
		String recordtime = request.getParameter("recordtime");
		String demandcontent = request.getParameter("demandcontent");
		String demandprovider = request.getParameter("demandprovider");
		String detaliid = request.getParameter("detaliid");
		
		Detailedrequirement dddd=new Detailedrequirement();
		if(demandtheme!="") {
			dddd.setDemandtheme(demandtheme);
		}
		if(detaliid!="") {
			dddd.setDetaliid(detaliid);
		}
		if(customer!="") {
			dddd.setCustomerBean(icustomer.findById(customer).get());
		}
		if(correspondingopp!="") {
			dddd.setCorrespondingOpp(correspondingopp);
		}
		if(recordtime!="") {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			Date date;
			System.out.println("recordtime:"+recordtime);
			if(recordtime!="") {
			try {
				date = formatter.parse(recordtime);
				dddd.setRecordtime(date);
			} catch (ParseException e) {
				
				e.printStackTrace();
				System.out.println("recordtime转换错误");
			}
			}
		}
		if(demandcontent!="") {
			dddd.setDemandcontent(demandcontent);
		}
		if(demandprovider!="") {
			dddd.setDemandprovider(demandprovider);
		}
		dddd.setShanchu(0);
		saser.xiugai222(dddd);
		return "shouqian/kehuxuqiu";
		
	}
}
